{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一步，导入Numpy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numpy数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1.0, 2.0, 3.0])\n",
    "y = np.array([[1,2,3], [4,5,6]])\n",
    "\n",
    "print(x)\n",
    "print(y)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3])\n",
    "b = np.array([1,5,2])\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)\n",
    "print(a**b)\n",
    "print(a//b)\n",
    "print(\"\\n多维数组\")\n",
    "a = np.random.rand(2,3)\n",
    "b = np.random.rand(2,3)\n",
    "print(a)\n",
    "print(b)\n",
    "print(\"运算结果\")\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)\n",
    "print(a**b)\n",
    "print(a//b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 广播（Broadcast）\n",
    "广播遵循的原则：\n",
    "1. 由右至左逐位比较；\n",
    "2. 当双方维数相同或者有一方为1时，继续比较；否则返回异常；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3])\n",
    "b = np.array([[3,4,5],[4,5,6]])\n",
    "print(a)\n",
    "print(b)\n",
    "print(\"\\n结果是\\n\", a+b)\n",
    "\n",
    "a = np.array([10,20,30])\n",
    "b=10\n",
    "\n",
    "print(\"\\n结果是\\n\", a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引与切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基础索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([0,1,2,3,4,5])\n",
    "print(a)\n",
    "print(a[0])\n",
    "print(a[1])\n",
    "print(a[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])\n",
    "print(a)\n",
    "\n",
    "print(a[0])\n",
    "print(a[0][0])\n",
    "print(a[1][2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 切片索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([0,1,2,3,4,5])\n",
    "a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])\n",
    "\n",
    "print(a[0:1])\n",
    "print(a[1:4])\n",
    "\n",
    "print(a[:])\n",
    "print(a[0:])\n",
    "print(a[:3])\n",
    "\n",
    "print(a[:-1])\n",
    "print(a[3:-2])\n",
    "\n",
    "print(a[:,0])\n",
    "\n",
    "print(a[... ,0])\n",
    "\n",
    "a=np.random.rand(3,3,3)\n",
    "print(a)\n",
    "print(a[:, :, 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([0,1,2,3])\n",
    "idx = []\n",
    "for i in np.arange(4):\n",
    "    rdn = np.random.rand(1)\n",
    "    if rdn>0.5:\n",
    "        idx.append(True)\n",
    "    else:\n",
    "        idx.append(False)\n",
    "print(idx)\n",
    "print(a[idx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mf:\\route\\读书笔记\\鱼书\\Numpy.ipynb Cell 18'\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/f%3A/route/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/%E9%B1%BC%E4%B9%A6/Numpy.ipynb#ch0000017?line=0'>1</a>\u001b[0m a\u001b[39m=\u001b[39mnp\u001b[39m.\u001b[39marray([\u001b[39m-\u001b[39m\u001b[39m2\u001b[39m,\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m,\u001b[39m0\u001b[39m,\u001b[39m1\u001b[39m,\u001b[39m2\u001b[39m])\n\u001b[0;32m      <a href='vscode-notebook-cell:/f%3A/route/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/%E9%B1%BC%E4%B9%A6/Numpy.ipynb#ch0000017?line=1'>2</a>\u001b[0m \u001b[39mprint\u001b[39m(a\u001b[39m>\u001b[39m\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m)\n\u001b[0;32m      <a href='vscode-notebook-cell:/f%3A/route/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/%E9%B1%BC%E4%B9%A6/Numpy.ipynb#ch0000017?line=2'>3</a>\u001b[0m \u001b[39mprint\u001b[39m(a[a\u001b[39m>\u001b[39m\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m])\n",
      "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "a=np.array([-2,-1,0,1,2])\n",
    "print(a>=0)\n",
    "print(a[a>=0])\n",
    "tmp = np.where(a>=0)\n",
    "\n",
    "b = np.array([i for i in a if i>=0])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MATHScore=np.array([99,90,100])\n",
    "name=np.array([\"Shang\",\"Li\",\"Tan\"])\n",
    "idx=name==\"Shang\"\n",
    "print(idx)\n",
    "print(MATHScore[idx])\n",
    "print(MATHScore[~idx])\n",
    "idx = ~idx\n",
    "print(MATHScore[idx])\n",
    "\n",
    "a = np.array([10])\n",
    "print(a)\n",
    "print(~a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 循环迭代访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([0,1,2,3,4,5])\n",
    "a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])\n",
    "for i in a:\n",
    "    print(i)\n",
    "\n",
    "for i in a:\n",
    "    for j in i:\n",
    "        print(j)\n",
    "\n",
    "print(\"***\")\n",
    "# a = a.T\n",
    "for i in np.nditer(a): # 按照内存中的存储位置来索引\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 花式索引（神奇索引）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.empty((8, 4))\n",
    "for i in np.arange(8):\n",
    "    a[i] = i # 广播: 赋值运算符也会发生广播\n",
    "print(a)\n",
    "\n",
    "idx = [0,1,2]\n",
    "# print(a[idx])\n",
    "\n",
    "idx=[0,0,-1]\n",
    "# print(a[idx])\n",
    "\n",
    "idx=[-1,1,2,3,4,5,6,0]\n",
    "# print(a[idx])\n",
    "\n",
    "idx=[[[0,1,2]]]\n",
    "# print(a[idx])\n",
    "\n",
    "# idx = a>-1\n",
    "# tmp = np.where(idx[:,0]==True)\n",
    "# idx = [tmp[0]]\n",
    "# print(a[[idx]])\n",
    "\n",
    "# print(a.reshape(1,1,8,4))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(32).reshape(8, 4)\n",
    "print(a)\n",
    "\n",
    "idx = [\n",
    "    [0,1,2],\n",
    "    [0,1,2]\n",
    "]\n",
    "print(a[idx])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(6).reshape(2, 3)\n",
    "print(a)\n",
    "print(np.sum(a))\n",
    "print(np.sum(a, axis=0))\n",
    "print(np.sum(a, axis=1))\n",
    "\n",
    "a=np.array([1,2,3])\n",
    "print(np.sum(a, axis=0)) # 一维只有一个方向\n",
    "# print(np.sum(a, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "print(a)\n",
    "print(b)\n",
    "\n",
    "print(\"按位乘\")\n",
    "print(a*b)\n",
    "\n",
    "print(\"点积\")\n",
    "print(np.dot(a,b))\n",
    "# print(np.dot(b,a))\n",
    "# print(a@b) # 中缀操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.random.randn(10, 5)\n",
    "print(a)\n",
    "\n",
    "print(a.size)\n",
    "print(a.shape)\n",
    "print(a.ndim)\n",
    "\n",
    "a=a.reshape(2,25)\n",
    "print(a)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "9775b82d8eac0bdc77741adceff29c45cb68e9290acb590ea3353a5619486182"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 ('anodet_env')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
